﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using DataMongo;
using Google.GData.Client;
using DataMongo.Entities;

using MVCMongo.Services;
using MVCMongo.ViewModel;
using WebMatrix.WebData;

namespace MVCMongo.Controllers
{
    public class HomeController : Controller
    {
        private readonly int _userId = WebSecurity.CurrentUserId;
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            var connection = new RepositoryBaseMongo("FeedPost");
            var viewmodel = new FeedListViewModel();
            viewmodel.FeedList = (from f in connection.GetAll<FeedPost>()
                                  where f.ImageURL1 != null
                                  select f).OrderByDescending(x => x.FeedPostId).Take(20).ToList();
            viewmodel.UpComingFeedList = connection.GetAll<FeedPost>().OrderByDescending(x => x.IsUpcoming).Take(20);
            return View(viewmodel);
        }

        public ActionResult Latest()
        {
            return View();
        }

        public ActionResult Hot()
        {
            return View();
        }

        public ActionResult Upcoming()
        {
            return View();
        }

        public ActionResult Stories()
        {
            return View();
        }

        public ActionResult Website()
        {
            return View();
        }
        
        public ActionResult Category(int id, string categoryname)
        {
            ViewBag.CategoryId = id;
            return View();
        }

        public ActionResult CategoryList()
        {
            var connection = new RepositoryBaseMongo("Category");
            var list = connection.GetAll<Category>();
            return PartialView(list);
        }

        public ActionResult Feed(int id, string title)
        {
            var connection = new RepositoryBaseMongo("FeedPost");

            List<FeedPost> list = (from f in connection.GetAll<FeedPost>()
                                   where f.FeedPostId == id
                                   select f).ToList();

            FeedPost feedpost = new FeedPost();
            if (list.Count > 0) feedpost = list[0];

            ViewBag.FeedUrl = feedpost.SourceLink;
            ViewBag.FeedTitle = feedpost.Title;
            return View();

        }

        public ActionResult FeedComment(int id, string title)
        {
            var connection = new RepositoryBaseMongo("FeedPost");
            List<FeedPost> list = (from f in connection.GetAll<FeedPost>()
                                   where f.FeedPostId == id
                                   select f).ToList();

            FeedPost feedpost = new FeedPost();
            if (list.Count > 0) feedpost = list[0];
            ViewBag.FeedUrl = feedpost.SourceLink;
            ViewBag.FeedTitle = feedpost.Title;
            feedpost.NumberofViews += 1;
            connection.InsertOrUpdate(feedpost);
            return View(feedpost);

        }

        public ActionResult GetFeedComments(int feedid)
        {
            var connection = new RepositoryBaseMongo("FeedComment");
            var viewModel = new FeedCommentViewModel();
            viewModel.ParentComments = (from f in connection.GetAll<FeedComment>()
                                        where f.FeedId == feedid && f.ReplyCommentId == 0
                                        select f).ToList();

            viewModel.ChildcomComments = (from f in connection.GetAll<FeedComment>()
                                          where f.FeedId == feedid && f.ReplyCommentId != 0
                                          select f).ToList();
            return PartialView(viewModel);
        }

        public ActionResult WriteComment(int feedId)
        {
            var comment = new FeedComment();
            comment.FeedId = feedId;
            return PartialView(comment);
        }

        public ActionResult ReplyComment(int commentId, int feedId)
        {
            var comment = new FeedComment();
            comment.FeedId = feedId;
            comment.ReplyCommentId = commentId;
            return View(comment);
        }

        [HttpPost]
        public ActionResult ReplyComment(FeedComment feedcomment)
        {
            var connection = new RepositoryBaseMongo("FeedComment");
            feedcomment.CommentedOn = DateTime.UtcNow;
            int Id = 0;
            var Feedlist = connection.GetAll<FeedComment>().ToList();
            if (Feedlist.Any())
                Id = Feedlist.Max(x => x.CommentId);
            Id += 1;
            feedcomment.CommentId = Id;
            connection.InsertOrUpdate(feedcomment);
            return RedirectToAction("FeedComment", "Home", new { feedId = feedcomment.FeedId });
        }


        [HttpPost]
        public ActionResult WriteComment(FeedComment feedcomment)
        {
            var connection = new RepositoryBaseMongo("FeedComment");
            feedcomment.CommentedOn = DateTime.UtcNow;
            int Id = 0;
            var Feedlist = connection.GetAll<FeedComment>().ToList();
            if (Feedlist.Any())
                Id = Feedlist.Max(x => x.CommentId);
            Id += 1;
            feedcomment.CommentId = Id;
            connection.InsertOrUpdate(feedcomment);
            return RedirectToAction("FeedComment", "Home", new { feedId = feedcomment.FeedId });
        }

        public ActionResult LatestFeeds()
        {
            var connection = new RepositoryBaseMongo("FeedPost");
            var data = connection.GetAll<FeedPost>().ToList().OrderByDescending(x => x.FeedPostId).Take(5);
            return PartialView(data);
        }

        public ActionResult RelatedFeed(int categoryId, int feedpostId)
        {
            var connection = new RepositoryBaseMongo("FeedPost");
            List<FeedPost> list = (from f in connection.GetAll<FeedPost>()
                                   where f.CategoryId == categoryId && f.FeedPostId != feedpostId
                                   select f).ToList();

            return PartialView(list.Take(2));
        }



        public ActionResult AllFeeds()
        {
            var connection = new RepositoryBaseMongo("FeedPost");
            var data = connection.GetAll<FeedPost>().ToList();
            return View(data);

        }

        public ActionResult Sources()
        {
            var connection = new RepositoryBaseMongo("Source");
            var data = connection.GetAll<Source>();
            return View(data);
        }

        public ActionResult SourceDetails(int id, string source)
        {
            var connection = new RepositoryBaseMongo("FeedPost");
            List<FeedPost> list = (from f in connection.GetAll<FeedPost>()
                                   where f.SourceId == id
                                   select f).ToList();

            var connectionsource = new RepositoryBaseMongo("Source");
            List<Source> listsource = (from f in connectionsource.GetAll<Source>()
                                       where f.SourceId == id
                                       select f).ToList();
            Source sources = new Source();
            if (list.Count > 0) sources = listsource[0];
            ViewBag.SourceName = sources.ShortDescription;
            return View(list);
        }


        public ActionResult Tags()
        {
            var connection = new RepositoryBaseMongo("Category");
            var data = connection.GetAll<Category>();
            return View(data);
        }

        public ActionResult TagDetails(int id, string source)
        {
            var connection = new RepositoryBaseMongo("FeedPost");
            List<FeedPost> list = (from f in connection.GetAll<FeedPost>()
                                   where f.CategoryId == id
                                   select f).ToList();

            var connectionsource = new RepositoryBaseMongo("Category");
            List<Category> listsource = (from f in connectionsource.GetAll<Category>()
                                         where f.CategoryId == id
                                         select f).ToList();
            Category sources = new Category();
            if (list.Count > 0) sources = listsource[0];
            ViewBag.SourceName = sources.ShortDescription;
            return View(list);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public JsonResult FeedLike(int feedId)
        {
            if (Request.IsAuthenticated)
            {
                var connection = new RepositoryBaseMongo("FeedLikes");
                var likelist = connection.GetAll<FeedLikes>().ToList();
                int Id = 0;
                if (likelist.Any())
                    Id = likelist.Max(x => x.FeedLikesId);
                Id += 1;
                var feedlike = new FeedLikes();
                feedlike.FeedPostId = feedId;
                feedlike.FeedLikesId = Id;
                feedlike.isLiked = true;
                feedlike.UserId = WebSecurity.CurrentUserId;

                connection.InsertOrUpdate(feedlike);
                var feedlikecount = UtilityService.GetFeedLikeCount(feedlike.FeedPostId);
                return Json(new { feedlike = feedlikecount, text = "Unlike" }, JsonRequestBehavior.AllowGet);
            }

            return Json(new { authenticated = "false" }, JsonRequestBehavior.AllowGet);


        }

        public JsonResult FeedUnlike(int feedId)
        {
            var connection = new RepositoryBaseMongo("FeedLikes");
            var feedlike = connection.GetAll<FeedLikes>().FirstOrDefault(x => x.FeedPostId == feedId && x.UserId == _userId);
            connection.Delete<FeedLikes>(feedlike._id);
            var feedlikecount = UtilityService.GetFeedLikeCount(feedlike.FeedPostId);
            return Json(new { feedlike = feedlikecount, text = "Like" }, JsonRequestBehavior.AllowGet);

        }
    }
}
